﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MySql.Data.MySqlClient;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TravellerApp1.Views
{
    /// <summary>
    /// UserWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserWindow : Window
    {
        public UserWindow()
        {
            InitializeComponent();
        }

        private void r1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string flightNum = tb_flight.Text;
                string cmd1 = "select * from flights where flightNum='" + flightNum + "';";
                int avail = -1;
                string fromcity = "";
                string tocity = "";
                MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
                while(reader.Read())
                {
                    avail = int.Parse(reader[3].ToString());
                    fromcity = reader[4].ToString();
                    tocity = reader[5].ToString();
                }
                MysqlHelper.CloseCon();
                if(avail <= 0)
                {
                    MessageBox.Show("没有空位");
                    return;
                }
                avail--;
                string cmd2 = "update flights set numAvail=" + avail.ToString() + " where flightNum='" + flightNum + "';";

                string resvKey = fromcity + ":" + tocity + ":" + WinHelper.GetId() + ":1";
                string cmd3 = "insert into reservations values('" + WinHelper.GetId() + "',1,'" + resvKey + "');";
                MysqlHelper.ExecuteSqlScript(cmd3, 2);
                MysqlHelper.ExecuteSqlScript(cmd2, 2);
                MessageBox.Show("预订成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void r2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string location = tb_bus.Text;
                string cmd1 = "select * from bus where location='" + location + "';";
                int avail = -1;
                MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
                while (reader.Read())
                {
                    avail = int.Parse(reader[3].ToString());
                }
                MysqlHelper.CloseCon();
                if (avail <= 0)
                {
                    MessageBox.Show("没有空位");
                    return;
                }
                avail--;
                string cmd2 = "update bus set numAvail=" + avail.ToString() + " where location='" + location + "';";

                string resvKey = location + ":" + WinHelper.GetId() + ":3";
                string cmd3 = "insert into reservations values('" + WinHelper.GetId() + "',3,'" + resvKey + "');"; //3为大巴车
                MysqlHelper.ExecuteSqlScript(cmd3, 2);
                MysqlHelper.ExecuteSqlScript(cmd2, 2);
                MessageBox.Show("预订成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void r3_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string location = tb_hotel.Text;
                string cmd1 = "select * from hotels where location='" + location + "';";
                int avail = -1;
                MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
                while (reader.Read())
                {
                    avail = int.Parse(reader[3].ToString());
                }
                MysqlHelper.CloseCon();
                if (avail <= 0)
                {
                    MessageBox.Show("没有空房间");
                    return;
                }
                avail--;
                string cmd2 = "update hotels set numAvail=" + avail.ToString() + " where location='" + location + "';";

                string resvKey = location + ":" + WinHelper.GetId() + ":2";
                string cmd3 = "insert into reservations values('" + WinHelper.GetId() + "',2,'" + resvKey + "');"; //2为宾馆房间
                MysqlHelper.ExecuteSqlScript(cmd3, 2);
                MysqlHelper.ExecuteSqlScript(cmd2, 2);
                MessageBox.Show("预订成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void btn_check_Click(object sender, RoutedEventArgs e)
        {
            bool resvflight = false;
            bool resvhotel = false;
            bool resvbus = false;
            string from = tb_from.Text;
            string to = tb_to.Text;
            string key1 = from + ":" + to + ":" + WinHelper.GetId() + ":1";
            string key2 = to + ":" + WinHelper.GetId() + ":2";
            string key3 = to + ":" + WinHelper.GetId() + ":3";
            string cmd1 = "select * from reservations where resvKey='" + key1 + "';";
            string cmd2 = "select * from reservations where resvKey='" + key2 + "';";
            string cmd3 = "select * from reservations where resvKey='" + key3 + "';";

            MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
            string tmp = "";
            while(reader.Read())
            {
                tmp = reader[0].ToString();
            }
            MysqlHelper.CloseCon();
            if (tmp == WinHelper.GetId())
                resvflight = true;

            reader = MysqlHelper.ExecuteSqlScript(cmd2);
            tmp = "";
            while (reader.Read())
            {
                tmp = reader[0].ToString();
            }
            MysqlHelper.CloseCon();
            if (tmp == WinHelper.GetId())
                resvhotel = true;

            reader = MysqlHelper.ExecuteSqlScript(cmd3);
            tmp = "";
            while (reader.Read())
            {
                tmp = reader[0].ToString();
            }
            MysqlHelper.CloseCon();
            if (tmp == WinHelper.GetId())
                resvbus = true;

            string msg = "";
            if (!resvflight)
                msg += "未预订航班 ";
            if (!resvhotel)
                msg += "未预订宾馆房间 ";
            if (!resvbus)
                msg += "未预订大巴车";
            if(resvflight && resvhotel && resvbus)
            {
                MessageBox.Show("路线完整");
            }
            else
            {
                MessageBox.Show(msg);
            }
        }

        private void enter_query_Click(object sender, RoutedEventArgs e)
        {
            GotoQueryWindow(this);
        }

        public static void GotoQueryWindow(UserWindow current)
        {
            QueryWindow login1 = new();
            login1.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            login1.Show();
            WinHelper.ForceClose(current);
        }

        private Dictionary<string, string> CityDictionary = new();

        private void enter_query1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String cmd1 = "select resvKey from RESERVATIONS where resvType=1 and custID='" + WinHelper.GetId() + "' order by time1;";
                MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
                CityDictionary.Clear();
                while (reader.Read())
                {
                    string resvKey = reader[0].ToString();
                    string[] sArry = resvKey.Split(':');
                    CityDictionary.Add(sArry[0], sArry[1]);
                }
                string rst = "";
                string last = "";
                foreach (KeyValuePair<string, string> kvp in CityDictionary)
                {
                    if (last == kvp.Key)
                    {
                        rst += " -> " + kvp.Value;
                    }
                    else
                    {
                        rst += "\n" + kvp.Key + " -> " + kvp.Value;
                    }

                    last = kvp.Value;
                }
                MessageBox.Show(rst, "旅行线路");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }
    }
}
